package com.mobile.service.impl;

import com.mobile.service.convert.ProcessFlowConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.ProcessFlowDTO;
import com.mobile.service.dto.ProcessFlowQueryDTO;
import com.mobile.service.entity.ProcessFlow;
import com.mobile.service.inter.ProcessFlowService;
import com.mobile.service.mapper.ProcessFlowMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 流程表service实现类
 */
@Service("processFlowService")
@Transactional
public class ProcessFlowServiceImpl implements ProcessFlowService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private ProcessFlowConverter processFlowConverter;
    @Autowired
    private ProcessFlowMapper processFlowMapper;

    @Override
    public Map<String, Object> addProcessFlow(ProcessFlowDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        processFlowMapper.insertProcessFlow(processFlowConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<ProcessFlowDTO> getProcessFlowListPage(ProcessFlowQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<ProcessFlowDTO> list = processFlowMapper.processFlowlistPage(queryDTO);
        return new PageResult<ProcessFlowDTO>(list);
    }

    @Override
    public PageResult<ProcessFlowDTO> getAuditingWorkOrders(ProcessFlowQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<ProcessFlowDTO> list = processFlowMapper.getAuditingWorkOrders(queryDTO);
        return new PageResult<ProcessFlowDTO>(list);
    }



    @Override
    public ProcessFlowDTO getProcessFlowById(Integer id) throws Exception {
        return processFlowMapper.getProcessFlowById(id);
    }

    @Override
    public Map<String, Object> updateProcessFlow(ProcessFlowDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Integer pkValue = dto.getProcessId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        ProcessFlowDTO data = this.getProcessFlowById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        processFlowMapper.updateProcessFlow(processFlowConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        return map;
    }

    @Override
    public Map<String, Object> removeProcessFlow(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        processFlowMapper.deleteProcessFlow(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ProcessFlowDTO> getProcessFlowList(ProcessFlowQueryDTO queryDTO) throws Exception {
        return processFlowMapper.getProcessFlowList(queryDTO);
    }

    @Override
    public void insertProcessFlow(ProcessFlow processFlow) throws Exception {
        processFlowMapper.insertProcessFlow(processFlow);
    }
    
    @Override
    public Map<String, Object> getTaskStatistics(ProcessFlowQueryDTO queryDTO) throws Exception {
        return processFlowMapper.getTaskStatistics(queryDTO);
    }
}
